//商家详情展示
function shopShowDetail(shopId) {
    $.ajax({
        url : "shop/query",
        async : true,
        type : "GET",
        data : {
            "shopId" : shopId
        },
        // 成功后开启模态框
        success : shopQueryDetail,
        error : function(res) {
            alert("商家信息获取失败");
        },
        dataType : "json"
    });
}
function shopQueryDetail(res) {
    if(res.code!=0){
        alert(res.msg);
    }
    var model = res.data;
    $("#d_shopName").text(model.shopName);
    $("#d_shopDesc").text(model.shopDesc);
    $("#d_shopPic").attr('src',model.shopPic);
    $("#d_shopAddr").text(model.shopAddr);
    $("#d_shopTime").text(model.shopTime);
    $("#d_modifyTime").text(model.modifyTime);
    $("#d_businessHours").text(model.businessHours);
    $("#d_servicePhone").text(model.servicePhone);
    $("#d_ownerName").text(model.ownerName);
    $("#d_ownerPhone").text(model.ownerPhone);
    if(model.shopScore===null){
        $("#d_shopScore").text("暂无");
    }else{
        $("#d_shopScore").text(model.shopScore);
    }
    // 显示模态框
    $('#shopDetailModal').modal('show');
}

//商家编辑展示
function shopShowEdit(shopId) {
    $.ajax({
        url : "shop/query",
        async : true,
        type : "GET",
        data : {
            "shopId" : shopId
        },
        // 成功后开启模态框
        success : shopQueryEdit,
        error : function(res) {
            alert("连接失败");
        },
        dataType : "json"
    });
}
function shopQueryEdit(res) {
    if(res.code!=0){
        alert(res.msg);
    }
    var model = res.data;
    $("#e_shopName").val(model.shopName);
    $("#e_shopDesc").val(model.shopDesc);
    $("#e_businessHours").val(model.businessHours);
    $("#e_servicePhone").val(model.servicePhone);
    $("#e_ownerName").val(model.ownerName);
    $("#e_ownerPhone").val(model.ownerPhone);
    $("#e_address").val(model.shopAddr);
    $("#e_lnglat").val(model.shopLng+','+model.shopLat);
    $("#e_shopId").val(model.shopId);

    //显示地图
    var map = new AMap.Map('mapContainer', {
        zoom:11,//级别
        center: [model.shopLng, model.shopLat],//中心点坐标
    });
    //实例化点标记
    var marker = new AMap.Marker({
        position:[model.shopLng, model.shopLat]//位置
    })
    map.add(marker);
    //地址经纬度转换
    var geocoder = new AMap.Geocoder();
    function geoCode() {
        var address  = $('#e_address').val();
        geocoder.getLocation(address, function(status, result) {
            if (status === 'complete'&&result.geocodes.length) {
                var lnglat = result.geocodes[0].location
                document.getElementById('e_lnglat').value = lnglat;
                marker.setPosition(lnglat);
                map.add(marker);
                map.setFitView(marker);
            }else{
                alert("请输入正确地址！");
            }
        });
    }
    document.getElementById("geoButton").onclick = geoCode;
    document.getElementById("e_address").onkeydown = function(e) {
        if (e.keyCode === 13) {
            geoCode();
            return false;
        }
        return true;
    };
    //为地图注册click事件获取鼠标点击出的经纬度坐标
    var clickEventListener = map.on('click', function(e) {
        document.getElementById("e_lnglat").value = e.lnglat.getLng() + ',' + e.lnglat.getLat();
        if (marker) {
            marker.setMap(null);
            marker = null;
        }
        addMarker(e.lnglat.getLng(),e.lnglat.getLat());
        //这边是数组
        var lnglatXY=[e.lnglat.getLng(),e.lnglat.getLat()];
        regeocoder(lnglatXY);
    });
    //更改点标记位置
    function addMarker(lng,lat) {
        marker = new AMap.Marker({
            icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
            position: [lng, lat]
        });
        marker.setMap(map);
    }
    //坐标-->地址
    function regeocoder(lnglatXY) {  //逆地理编码
        var geocoder = new AMap.Geocoder({
            radius: 100,
            extensions: "all"
        });
        geocoder.getAddress(lnglatXY, function(status, result) {
            if (status === 'complete' && result.info === 'OK') {
                geocoder_CallBack(result);
            }
        });
    }
    function geocoder_CallBack(data) {
        var address = data.regeocode.formattedAddress; //返回地址描述
        document.getElementById("e_address").value=address;
    }

    // 显示模态框
    $('#shopEditModal').modal('show');
}
//商家编辑提交
$("#shopEditButton").click(function () {
    var data = new FormData(document.getElementById("shopEditForm"));
    $.ajax({
        type: 'post',
        url: 'shop/modify',
        dataType: 'json',
        data: data,
        contentType: false, //不设置内容类型
        processData: false, //不处理数据
        error: function(request) {
            alert("未知错误，请检查输入格式！");
        },
        success: function(res) {
            if(res.code==0) {
                alert("修改商家信息成功！");
                window.location.reload();
            }else{
                alert(res.msg);
            }
        }
    });
});

//商家删除
function shopShowDelete(shopId) {
    if (confirm("商家删除后将删除所有相关商品，请检查该商家名下的商品，慎重删除商家。\n\r是否确认删除该商家？")) {
        $.ajax({
            type: 'get',
            url: 'shop/delete',
            dataType: 'json',
            data: {
                "shopId": shopId
            },
            contentType: false, //不设置内容类型
            error: function (request) {
                alert("未知错误，删除失败");
            },
            success: function (res) {
                if (res.code == 0) {
                    alert("删除商家成功！");
                    window.location.reload();
                } else {
                    alert(res.msg);
                }
            }
        });
    }
}

//商家添加展示
function shopShowAdd() {
    /**
     *地图模块
     */
        //显示地图
    var map = new AMap.Map('a_mapContainer', {
            zoom:11,//级别
            center: [120.2, 30.3],//中心点坐标
        });
    // 实例化点标记
    var marker = new AMap.Marker({
        position:[120.2, 30.3]
    })
    //地址经纬度转换
    var geocoder = new AMap.Geocoder();
    function geoCode() {
        var address  = $('#a_address').val();
        geocoder.getLocation(address, function(status, result) {
            if (status === 'complete'&&result.geocodes.length) {
                var lnglat = result.geocodes[0].location
                document.getElementById('a_lnglat').value = lnglat;
                marker.setPosition(lnglat);
                map.add(marker);
                map.setFitView(marker);
            }else{
                log.error('根据地址查询位置失败');
            }
        });
    }
    document.getElementById("a_geoButton").onclick = geoCode;
    document.getElementById("a_address").onkeydown = function(e) {
        if (e.keyCode === 13) {
            geoCode();
            return false;
        }
        return true;
    };
    //为地图注册click事件获取鼠标点击出的经纬度坐标
    var clickEventListener = map.on('click', function(e) {
        document.getElementById("a_lnglat").value = e.lnglat.getLng() + ',' + e.lnglat.getLat();
        if (marker) {
            marker.setMap(null);
            marker = null;
        }
        addMarker(e.lnglat.getLng(),e.lnglat.getLat());
        //这边是数组
        var lnglatXY=[e.lnglat.getLng(),e.lnglat.getLat()];
        regeocoder(lnglatXY);
    });
    //更改点标记位置
    function addMarker(lng,lat) {
        marker = new AMap.Marker({
            icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
            position: [lng, lat]
        });
        marker.setMap(map);
    }
    //坐标-->地址
    function regeocoder(lnglatXY) {  //逆地理编码
        var geocoder = new AMap.Geocoder({
            radius: 100,
            extensions: "all"
        });
        geocoder.getAddress(lnglatXY, function(status, result) {
            if (status === 'complete' && result.info === 'OK') {
                geocoder_CallBack(result);
            }
        });
    }
    function geocoder_CallBack(data) {
        var address = data.regeocode.formattedAddress; //返回地址描述
        document.getElementById("a_address").value=address;
    }

    // 显示模态框
    $('#shopAddModal').modal('show');
}

//商家添加提交
$("#shopAddButton").click(function () {
    var data = new FormData(document.getElementById("shopAddForm"));
    //判断是否为空
    if(data.get('shopName')===null||data.get('shopName')===undefined||data.get('shopName')==''||
        data.get('shopDesc')===null||data.get('shopDesc')===undefined||data.get('shopDesc')==''||
        data.get('businessHours')===null||data.get('businessHours')===undefined||data.get('businessHours')==''||
        data.get('servicePhone')===null||data.get('servicePhone')===undefined||data.get('servicePhone')==''||
        data.get('ownerName')===null||data.get('ownerName')===undefined||data.get('ownerName')==''||
        data.get('ownerPhone')===null||data.get('ownerPhone')===undefined||data.get('ownerPhone')==''||
        data.get('file')===null||data.get('file')===undefined||data.get('file')==''||
        data.get('shopAddr')===null||data.get('shopAddr')===undefined||data.get('shopAddr')==''||
        data.get('lnglat')===null||data.get('lnglat')===undefined||data.get('lnglat')==''){
        alert("数据不能为空，请检查。");
        return 0;
    }
    $.ajax({
        type: 'post',
        url: 'shop/add',
        dataType: 'json',
        data: data,
        contentType: false, //不设置内容类型
        processData: false, //不处理数据
        error: function(request) {
            alert("未知错误，请检查输入格式！");
        },
        success: function(res) {
            if(res.code==0) {
                alert("添加商品成功！");
                window.location.reload();
            }else{
                alert(res.msg);
            }
        }
    });
});


//销售额展示
function showSales(shopId) {
    $.ajax({
        url : "sales/shop",
        async : true,
        type : "GET",
        data : {
            "shopId" : shopId
        },
        // 成功后开启模态框
        success : function(res){
            if(res.code!=0){
                alert(res.msg);
            }
            var model = res.data;
            $("#s_id").val(model.id);
            $("#s_name").text(model.name);
            $("#s_yearSale").text(model.yearSale +' 元');
            $("#s_monthSale").text(model.monthSale +' 元');
            $("#s_weekSale").text(model.weekSale +' 元');
            $("#s_daySale").text(model.daySale +' 元');
            //显示模态框
            $('#salesModal').modal('show');
        },
        error : function(res) {
            alert("销量信息获取失败");
        },
        dataType : "json"
    });
}


//查询时间段销售额
$("#saleButton").click(function () {
    var formdata = new FormData(document.getElementById("salesForm"));
    $.ajax({
        type: "POST",
        url:"sales/shop/search",
        data:formdata,	// 序列化表单值
        processData:false,
        contentType:false,
        error: function(request) {
            alert("未知错误！");
        },
        success: function(res) {
            if(res.code==0) {
                $("#s_sale").text(res.data +' 元');
            }else{
                alert(res.msg);
            }
        }
    });
});
